package com.wtwd.campus.jdzc.controller;

import com.wtwd.campus.jdzc.common.Constant;
import com.wtwd.campus.jdzc.common.RespCode;
import com.wtwd.campus.jdzc.common.RespEntity;
import com.wtwd.campus.jdzc.entity.JdzcStudent;
import com.wtwd.campus.jdzc.entity.JdzcUser;
import com.wtwd.campus.jdzc.service.*;
import com.wtwd.campus.service.RedisService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/2/14 16:32
 * @Description:
 */
@RestController
@RequestMapping("/jdzc/teacher")
public class JdzcTeacherController {


    private static final Logger logger = LoggerFactory.getLogger(JdzcTeacherController.class);


    @Autowired
    private RedisService redisService;

    @Autowired
    private JdzcService jdzcService;


    @Autowired
    private JdzcNotificationService jdzcNotificationService;


    @Autowired
    private JdzcClassService jdzcClassService;

    @Autowired
    private JdzcTeacherService jdzcTeacherService;

    @Autowired
    private JdzcStudentService jdzcStudentService;


    @Autowired
    private AuthService authService;


    /**
     * 获取一个老师管理的班级
     *
     * @param
     * @return
     */
    @GetMapping("/getClassName")
    public RespEntity getClassName(HttpServletRequest request) {
        logger.info("/getClassName");
        String token = request.getHeader(Constant.HEADER_TOKEN_KEY);
        String redisUserId = redisService.getValue(token);
        if (StringUtils.isBlank(redisUserId)) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        Integer userId = Integer.parseInt(redisUserId);
        //根据手机号码查询一个老师授课的班级信息
        String classes = jdzcService.getClassesMessByUserId(userId);
        if (StringUtils.isBlank(classes)) {
            return new RespEntity(RespCode.NO_DATA);
        }
        String[] classList = classes.split(",");
        List<Integer> classIdList = new ArrayList<>();
        for (String id : classList) {
            classIdList.add(Integer.valueOf(id));
        }
        //根据id获取班级信息
        List<Map<String, Object>> messList = jdzcService.getClassNameByClassIdList(classIdList);
        List<Map<String, Object>> result = new ArrayList<>();
        messList.stream().forEach(e -> {
            String className = (String) e.get("classes_name");
            String gradeName = (String) e.get("grade_name");
            Integer classId = (Integer) e.get("classes_id");
            Map<String, Object> map = new HashMap<>();
            map.put("name", gradeName.concat(className));
            map.put("classId", classId);
            result.add(map);
        });
        return new RespEntity(RespCode.SUCCESS, result);
    }

    /**
     * 根据班级id获取用户所管理的学生信息以及学生家长信息
     */

    @GetMapping("/getStudentInfoAndParents/{classId}")
    public RespEntity getStudentInfoAndParents(@PathVariable Integer classId, HttpServletRequest request) {
        String token = request.getHeader(Constant.HEADER_TOKEN_KEY);
        String redisUserId = redisService.getValue(token);
        if (StringUtils.isBlank(redisUserId)) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        Integer userId = Integer.parseInt(redisUserId);
        logger.info("/getStudentInfoAndParents userId={},classId={}", userId, classId);
        //校验该教师身份
        JdzcUser user = jdzcService.getWxUserById(userId);
        if (user == null) {
            return new RespEntity(RespCode.USER_NOT_FOUND);
        }
        Boolean authTeacher = authService.authTeacher(user.getPhone(), Constant.jdzcOrgCode);
        if (!authTeacher) {//校验教师是否存在,是否存在于交大组织中
            return new RespEntity(RespCode.NO_PERMISSION);
        }
        //校验该教师是否授课该班级
        Boolean authClass = authService.authClassAndTeacher(user.getPhone(), classId);
        if (!authClass) {
            return new RespEntity(RespCode.NO_PERMISSION);
        }
        //获取该班级的所有学生以及家长信息
        List<JdzcStudent> students = jdzcStudentService.getStudentListByClassId(classId);
        return new RespEntity(RespCode.SUCCESS,students);
    }

}
